package com.ruoyi.easyfame.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.easyfame.domain.Qna;
import com.ruoyi.easyfame.service.IQnaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专家问答Controller
 * 
 * @author xhb
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/easyfame/qna")
public class QnaController extends BaseController
{
    @Autowired
    private IQnaService qnaService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询专家问答列表
     */
    @PreAuthorize("@ss.hasPermi('easyfame:qna:list')")
    @GetMapping("/list")
    public TableDataInfo list(Qna qna)
    {
        startPage();
        List<Qna> list = qnaService.selectQnaList(qna);
        return getDataTable(list);
    }

    /**
     * 导出专家问答列表
     */
    @PreAuthorize("@ss.hasPermi('easyfame:qna:export')")
    @Log(title = "专家问答", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Qna qna)
    {
        List<Qna> list = qnaService.selectQnaList(qna);
        ExcelUtil<Qna> util = new ExcelUtil<Qna>(Qna.class);
        util.exportExcel(response, list, "专家问答数据");
    }

    /**
     * 获取专家问答详细信息
     */
    @PreAuthorize("@ss.hasPermi('easyfame:qna:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(qnaService.selectQnaById(id));
    }

    /**
     * 新增专家问答
     */
    @PreAuthorize("@ss.hasPermi('easyfame:qna:add')")
    @Log(title = "专家问答", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Qna qna)
    {
        return toAjax(qnaService.insertQna(qna));
    }

    /**
     * 修改专家问答
     */
    @PreAuthorize("@ss.hasPermi('easyfame:qna:edit')")
    @Log(title = "专家问答", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Qna qna)
    {
        return toAjax(qnaService.updateQna(qna));
    }

    /**
     * 删除专家问答
     */
    @PreAuthorize("@ss.hasPermi('easyfame:qna:remove')")
    @Log(title = "专家问答", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qnaService.deleteQnaByIds(ids));
    }

    // 在QnaController中添加或修改
    @PutMapping("/answer")
    public AjaxResult answerQna(@RequestBody Qna qna) {
        // 更新回答内容和状态
        qna.setAnswerTime(new Date());
        qna.setStatus(String.valueOf(1)); // 已回答状态
        return toAjax(qnaService.updateQna(qna));
    }
    // 在QnaController中添加
    @PutMapping("/solve")
    public AjaxResult solveQna(@RequestBody Qna qna) {
        qna.setStatus(String.valueOf(2)); // 已解决状态
        return toAjax(qnaService.updateQna(qna));
    }
    //获取全部用户
    @PreAuthorize("@ss.hasPermi('easyfame:qna:list')")
    @GetMapping("/listUser")
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

}
